#include "LUA_States.h"

int addState(lua_State* L){
	printf("call func: addState\n");
	const char* stateName = lua_tostring(L,-1);
	lua_pop(L,1);

	if(currentState ==""){
		currentState = (char*)stateName;
	}

	gameStateFunctions state;
	state.name = stateName;
	state.loopFunc = "";
	state.loadFunc = "";
	state.keyboardKeyDownFunc = "";
	state.keyboardKeyReleasedFunc = "";
	state.mouseButtonDownFunc = "";
	state.mouseButtonReleasedFunc = "";

	States[stateName] = state;

	return 0;
}

int registerLoadFunc(lua_State* L){
	printf("call func: registerLoadFunc\n");
	const char* loadFuncName = lua_tostring(L,-1);
	lua_pop(L,1);
	const char* stateName = lua_tostring(L,-1);
	lua_pop(L,1);

	gameStateFunctions state = States[stateName];
	if(state.name == NULL){
		printf("ERROR: registerLoadFunc, state not found\n");
		return 0;
	}
	state.loadFunc = loadFuncName;
	States[stateName]=state;

	return 0;
}

int registerLoopFunc(lua_State* L){
	printf("call func: registerLoopFunc\n");
	const char* loopFuncName = lua_tostring(L,-1);
	lua_pop(L,1);
	const char* stateName = lua_tostring(L,-1);
	lua_pop(L,1);

	gameStateFunctions state = States[stateName];
	if(state.name == NULL){
		printf("ERROR: registerLoopFunc, state not found\n");
		return 0;
	}
	state.loopFunc = loopFuncName;
	States[stateName]=state;

	return 0;
}

int setActiveState(lua_State* L){
	const char* s = lua_tostring(L,-1);
	currentState = (char*)s;
	return 0;
}

void callLoopFunction(lua_State* L){
	if(currentState != ""){
		gameStateFunctions state = States[currentState];	
		if(state.loopFunc != ""){
			lua_getglobal(L, state.loopFunc);		
			if (lua_pcall(L, 0, 0, 0) != 0)
		        	printf("error running function 'callLoopFunction': %s\n",
		                 lua_tostring(L, -1));
		}
		else printf("ERROR: loopFunction for state %s is missing\n",state.name);	
	}
	else printf("ERROR: no states defined\n");
}

int registerKeyboardKeyDownFunc(lua_State* L){
	printf("call func: registerKeyboardKeyDownFunc\n");
	const char* keyboardFuncName = lua_tostring(L,-1);
	lua_pop(L,1);
	const char* stateName = lua_tostring(L,-1);
	lua_pop(L,1);

	gameStateFunctions state = States[stateName];
	if(state.name == NULL){
		printf("ERROR: registerKeyboardKeyDownFunc, state not found\n");
		return 0;
	}
	state.keyboardKeyDownFunc = keyboardFuncName;
	States[stateName]=state;

	return 0;
}

int registerKeyboardKeyReleasedFunc(lua_State* L){
	printf("call func: registerKeyboardKeyReleasedFunc\n");
	const char* keyboardFuncName = lua_tostring(L,-1);
	lua_pop(L,1);
	const char* stateName = lua_tostring(L,-1);
	lua_pop(L,1);

	gameStateFunctions state = States[stateName];
	if(state.name == NULL){
		printf("ERROR: registerKeyboardKeyReleasedFunc, state not found\n");
		return 0;
	}
	state.keyboardKeyReleasedFunc = keyboardFuncName;
	States[stateName]=state;

	return 0;
}

int registerMouseButtonDownFunc(lua_State* L){	
	printf("call func: registerMouseButtonDownFunc\n");
	const char* mouseFuncName = lua_tostring(L,-1);
	lua_pop(L,1);
	const char* stateName = lua_tostring(L,-1);
	lua_pop(L,1);

	gameStateFunctions state = States[stateName];
	if(state.name == NULL){
		printf("ERROR: registerMouseButtonDownFunc, state not found\n");
		return 0;
	}
	state.mouseButtonDownFunc = mouseFuncName;
	States[stateName]=state;

	return 0;
}

int registerMouseButtonReleasedFunc(lua_State* L){	
	printf("call func: registerMouseButtonReleasedFunc\n");
	const char* mouseFuncName = lua_tostring(L,-1);
	lua_pop(L,1);
	const char* stateName = lua_tostring(L,-1);
	lua_pop(L,1);

	gameStateFunctions state = States[stateName];
	if(state.name == NULL){
		printf("ERROR: registerMouseButtonReleasedFunc, state not found\n");
		return 0;
	}
	state.mouseButtonReleasedFunc = mouseFuncName;
	States[stateName]=state;

	return 0;
}

void callKeyDownFunction(lua_State* L, char* key){
	if(currentState != ""){
		gameStateFunctions state = States[currentState];	
		if(state.keyboardKeyDownFunc != ""){
			lua_getglobal(L, state.keyboardKeyDownFunc);
			lua_pushstring(L, key);
			if (lua_pcall(L, 1, 0, 0) != 0)
		        	printf("error running function `f': %s\n",
		                 lua_tostring(L, -1));
		}
		//else printf("ERROR: loopFunction for state %s is missing\n",state.name);	
	}	
}

void callKeyReleasedFunction(lua_State* L, char* key){
	if(currentState != ""){
		gameStateFunctions state = States[currentState];	
		if(state.keyboardKeyReleasedFunc != ""){
			lua_getglobal(L, state.keyboardKeyReleasedFunc);
			lua_pushstring(L, key);
			if (lua_pcall(L, 1, 0, 0) != 0)
		        	printf("error running function `f': %s\n",
		                 lua_tostring(L, -1));
		}
		//else printf("ERROR: loopFunction for state %s is missing\n",state.name);	
	}	
}








